<?php

/**
 * Description of NewsletterHelper
 *
 * @author Jose Luis Quintana <joseluis@lbn.pe>
 * @param GalleryModel $GalleryModel
 */
class NewsletterHelper extends JLHelper {

    private $GalleryModel;
    private $_title;
    private $_month;
    private $_months;
    private $_base_url;

    public function __construct() {
        parent::__construct();

        include_once JLPATH_MODELS . DS . "GalleryModel.php";
        $this->GalleryModel = new GalleryModel();
    }

    public function setBaseURL($base_url) {
        $this->_base_url = $base_url;
    }
    
    public function setTitle($title) {
        $this->_title = $title;
    }

    public function setMonth($month) {
        $this->_month = $month;
    }

    public function setMonthActualites($months) {
        $this->_months = $months;
    }

    public function getGalleryById($id) {
        return $this->GalleryModel->getGalleryById($id);
    }

    public function getActualiteLink($actualite) {
        $url = empty($this->_base_url) ? $this->JL->getNameDomain() : $this->_base_url;
        $hash = "#";

        if ($actualite["int_id_section"] < 3) {
            $url .= "/actualites.html?act=" . $actualite["int_id_actualite"] . "&";
            $hash .= $this->_months[$actualite["var_monthyear"]];
        } else {
            $id = $actualite["int_id_section"] - 2;
            $url .= "/section.html?id=$id&act=" . $actualite["int_id_actualite"] . "&";
            $galleries = $this->GalleryModel->countGalleriesBySectionViewId($id);
            $galleries = $galleries["num_gall"];
            $hash .= ($galleries > 0) ? $galleries + 5 : 5;
        }

        $source = urlencode($this->_title);
        $medium = 'Email';
        $term = urlencode($this->_month);
        $content = urlencode($actualite["var_desc_section"] . ' : ' . $actualite["var_ac_title"]);
        $campaign = urlencode("Newsletter " . $this->_month . " " . date("Y"));
        return $url . "utm_source=$source&utm_medium=$medium&utm_term=$term&utm_content=$content&utm_campaign=$campaign$hash";
    }

    public function hasGalleries($string_gallery) {
        $string_gallery = unserialize($string_gallery);
        if(!empty($string_gallery) && is_array($string_gallery)){
            return $string_gallery;
        } else {
            return FALSE;
        }
    }

}